package com.ruoyi.student.controller;


import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.base.internal.Finalizer;
import com.ruoyi.classes.service.IXzkClassesService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.student.domain.XzkInfoStudent;
import com.ruoyi.student.service.IXzkInfoStudentService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.student.domain.XzkStudent;
import com.ruoyi.student.service.IXzkStudentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

/**
 * 学生信息Controller
 *
 * @author ruoyi
 * @date 2021-04-02
 */
@Controller
@RequestMapping("/student/student")
public class XzkStudentController extends BaseController
{
    private String prefix = "student/student";

    @Autowired
    private IXzkStudentService xzkStudentService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IXzkInfoStudentService xzkInfoStudentService;

    @Autowired
    private IXzkClassesService xzkClassesService;

    @RequiresPermissions("student:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }


    /***
     *  促课信息
     */
    @GetMapping("/getSuperviseInfo/{stuid}")
    public String getSuperviseInfo(@PathVariable("stuid") Long stuid, ModelMap mmap){
        XzkStudent xzkStudent = xzkStudentService.selectXzkStudentById(stuid);
        getColorType(xzkStudent);
        mmap.put("xzkStudent", xzkStudent);
        System.out.println(xzkStudent);
        return prefix + "/superviseClass";
    }

    /**
     * 查询促课结果
     */
    @RequiresPermissions("student:student:openhistory")
    @GetMapping("/openhistory/{stuid}")
    public String openhistory(@PathVariable("stuid") Long stuid, Model mmap) {
        List<XzkStudent> list = xzkStudentService.selectXzkStudentopenhistoryList(stuid);
        mmap.addAttribute("list",list);
        return prefix + "/superviseHistory";
    }

    /**
     * 前台模板
     */
    private void getColorType( XzkStudent student){
        if (student.getSuperviseclasslasttime()!=null){
            Date date=new Date();
            int days = (int) ((date.getTime() - student.getSuperviseclasslasttime().getTime()) / (1000*3600*24));
            if (days<=5){
                student.setColorType(1);
            }else if(days<=8){
                student.setColorType(2);
            }else {
                student.setColorType(3);
            }
        }else {
            student.setColorType(1);
        }
    }

    /**
     *   查询学生促课记录
     */
    @PostMapping( "/superviseClass")
    @ResponseBody
    public String superviseClass(@RequestParam("stuid") Long stuid,@RequestParam("promotion") String promotion){
        int result = xzkStudentService.superviseClass(stuid);
        xzkStudentService.installXzkStudentByPromotion(stuid,promotion);
        return "success";
    }


    /**
     * 查询学生信息列表
     */
    @RequiresPermissions("student:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(XzkStudent xzkStudent, HttpSession session)
    {
        String username = (String)session.getAttribute("userName");
        startPage();
        List<XzkStudent> list = xzkStudentService.selectXzkStudentList1(xzkStudent,username);
        for (XzkStudent student : list) {
            getColorType(student);
        }
        return getDataTable(list);
    }

//    /**
//     * 查询退休学学员信息
//     * */
//    @RequiresPermissions("student:student:list")
//    @PostMapping("/list2")
//    @ResponseBody
//    public TableDataInfo list2(XzkStudent xzkStudent)
//    {
//        startPage();
//        List<XzkStudent> list = xzkStudentService.selectXzkStudentList(xzkStudent);
//        for (XzkStudent student : list) {
//            getColorType(student);
//        }
//        return getDataTable(list);
//    }

//    @RequiresPermissions("student:student:list")
//    @PostMapping("/list3")
//    @ResponseBody
//    public TableDataInfo list3(XzkStudent xzkStudent)
//    {
//        startPage();
//        List<XzkStudent> list = xzkStudentService.selectXzkStudentList(xzkStudent);
//        for (XzkStudent student : list) {
//            getColorType(student);
//        }
//        return getDataTable(list);
//    }
    /**
     * 导出学生信息列表
     */
    @RequiresPermissions("student:student:export")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(XzkStudent xzkStudent) {
        List<XzkStudent> list = null;
        if (!StringUtils.isEmpty(xzkStudent.getStuids())){
            list = xzkStudentService.selectXzkStudentByIds(xzkStudent.getStuids());
        }else {
            list = xzkStudentService.selectXzkStudentList(xzkStudent);
        }

        ExcelUtil<XzkStudent> util = new ExcelUtil<XzkStudent>(XzkStudent.class);
        return util.exportExcel(list, "student");
    }

    /**
     * 导入学生信息列表
     */
    @RequiresPermissions("student:student:import")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/import")
    @ResponseBody
    public AjaxResult importExecl(@RequestParam("file") MultipartFile multipartFile, boolean updateSupport) throws Exception {
        ExcelUtil<XzkStudent> util = new ExcelUtil<XzkStudent>(XzkStudent.class);
        List<XzkStudent> studentList = util.importExcel(multipartFile.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = xzkStudentService.importStudent(studentList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 新增学生信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


    /**
     * 新增保存学生信息
     */
    @RequiresPermissions("student:student:add")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(XzkStudent xzkStudent) {
        return toAjax(xzkStudentService.insertXzkStudent(xzkStudent));
    }

    /**
     * 修改 学生信息
     */
    @GetMapping("/edit/{stuid}")
    public String edit(@PathVariable("stuid") Long stuid, ModelMap mmap) {
        XzkStudent xzkStudent = xzkStudentService.selectXzkStudentById(stuid);
        mmap.put("xzkStudent", xzkStudent);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生信息
     */
    @RequiresPermissions("student:student:edit")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(XzkStudent xzkStudent) {
        xzkStudent.setOpt(ShiroUtils.getLoginName());
        return toAjax(xzkStudentService.updateXzkStudent(xzkStudent));
    }

    /**
     * 删除学生信息
     */
    @RequiresPermissions("student:student:remove")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(xzkStudentService.deleteXzkStudentByIds(ids));
    }


    /**
     * 查询学生info信息列表
     */
    @RequiresPermissions("student:student:info")
    @GetMapping("/info")
    public String info(Long stuid, Model mmap) {
        List<XzkInfoStudent> list = xzkInfoStudentService.selectXzkInfoStudentByForId(stuid);
        mmap.addAttribute("list",list);
        return prefix + "/infoList";
    }

    /**
     * 查询班级信息是否存在
     */
    @RequiresPermissions("student:student:class")
    @PostMapping("/class")
    @ResponseBody
    public AjaxResult classByClassname(String classname)
    {
        startPage();
        String message = xzkClassesService.selectXzkClassesByClassName(classname).getClassname();
        return AjaxResult.success(message);
    }


    /**
     * 查询教学教务老师所带学员数量
     */
    @PostMapping("/countdata")
    @ResponseBody
    public List<Map<String,Object>> countdata(@Param("username") String username) {
        SysUser sysUser = sysUserService.selectUserByUserName(username);
        if (sysUser.getDeptId()==101){
            //教学老师所带学员总数量
            int count = xzkStudentService.selectStuCountByMasterTeacher(null, username,null);

        }else if (sysUser.getDeptId()==207){
            //教务老师所带学员总数量
        }

        //System.out.println("老师所带活跃人数=============="+count);
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("title","总人数");
        map.put("count",200);
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("title","活跃人数");
        map1.put("count",120);
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("title","失联人数");
        map2.put("count",50);
        HashMap<String, Object> map3 = new HashMap<>();
        map3.put("title","不活跃人数");
        map3.put("count",100);
        HashMap<String, Object> map4 = new HashMap<>();
        map4.put("title","休学人数");
        map4.put("count",10);
        list.add(map);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        return list;

    }
}
